﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using HZ.Supervise.Model.Enum;
using HZ.Supervise.DAL;

namespace HZ.Supervise.Web.Secondphase
{
    public partial class ProcureHospitalMax : BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.AreaControls1.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID;
                MaxforMonth1.Banding();
                Databing();
            }
        }

        private void Databing()//modify by 曹鹏 2011年5月18日16:49:553 医院排名数据绑定
        {
            try
            {

                StringBuilder wherePm = new StringBuilder();

                wherePm = HZ.Supervise.Web.Function.GetConditionOfAreaId(wherePm, this.AreaControls1.SelectedAreaID) as StringBuilder;

                wherePm.AppendFormat(" AND daytime='{0}'", this.MaxforMonth1.Getvalue());

                StringBuilder wherePms = new StringBuilder();

                wherePms = HZ.Supervise.Web.Function.GetConditionOfAreaId(wherePms, this.AreaControls1.SelectedAreaID) as StringBuilder;

                string _oldYearmonth = this.MaxforMonth1.Getvalue().Insert(4, "-") + "-01";//根据本月拼接字符串，以便于能够转化成datetime类型，从而能够调用datetime类中求得上个月的方法

                wherePms.AppendFormat("  AND daytime='{0}'", Convert.ToDateTime(_oldYearmonth).AddMonths(-1).ToString("yyyyMM"));
                //修改者:蒋峰(2011-06-23)
                //备  注:现在用不上
                string sortSource = "''";

                if (this.dropMort.SelectedValue == "0" || this.dropMort.SelectedValue == "1" || this.dropMort.SelectedValue == "2")
                {

                    wherePm.AppendFormat(" and SortSorce = {0}", this.dropMort.SelectedValue);

                    wherePms.AppendFormat(" and SortSorce = {0}", this.dropMort.SelectedValue);

                    sortSource = this.dropMort.SelectedValue;

                }

                string orderOfrank = "";

                string orderBy = "";

                if (this.ddlorderby.SelectedValue == "PurchaseAmount" || this.ddlorderby.SelectedValue == "DistributeAmount" || this.ddlorderby.SelectedValue == "WareHouseAmount")
                {

                    orderOfrank = " sum(" + this.ddlorderby.SelectedValue + ")";

                    orderBy = this.ddlorderby.SelectedValue + "total  " + this.dllorder.SelectedValue;

                }
                else
                {

                    if (this.ddlorderby.SelectedValue == "WareHouseRate")
                    {
                        orderOfrank = " (case when SUM(WarehouseAmount) = 0 then 0 when SUM(PurchaseAmount)=0 then 0 when CONVERT(decimal(18,3),CONVERT(float, SUM(WarehouseAmount)) / SUM(PurchaseAmount))>1 then 1 else  CONVERT(decimal(18,3),CONVERT(float, SUM(WarehouseAmount)) / SUM(PurchaseAmount)) end)";
                    }
                    else
                    {

                        orderOfrank = " (case when SUM(DistributeAmount) = 0 then 0 when SUM(PurchaseAmount)=0 then 0 when CONVERT(decimal(18,3),CONVERT(float, SUM(DistributeAmount)) / SUM(PurchaseAmount))>1 then 1 else CONVERT(decimal(18,3),CONVERT(float, SUM(DistributeAmount)) / SUM(PurchaseAmount)) end)";

                    }

                    orderBy = this.ddlorderby.SelectedValue + " " + this.dllorder.SelectedValue;

                }

                string whereHosName = "";

                if (!string.IsNullOrEmpty(this.txtHospitalname.Text.Trim()))
                {

                    whereHosName = "and A.HospitalName like '%" + txtHospitalname.Text.Trim() + "%'";

                }

                int totalrecord = 0;

                DataTable dt = new BLL.BLL_PreWarning().GeteHospsRanking1(orderOfrank, orderBy, wherePm.ToString(), wherePms.ToString(), sortSource, whereHosName, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalrecord);

                gvwProcureCatalog.Visible = true;

                gvwProcureCatalog2.Visible = false;

                Function.ChangeRate(dt, "WarehouseRate", "addWarehouseRate");

                Function.ChangeRate(dt, "DistributeRate", "addDistributeRate");

                Function.AddColumn(dt, "SortSource");

                Function.AddColRank(dt, "pms");

                gvwProcureCatalog.DataSource = dt;

                gvwProcureCatalog.DataBind();

                this.AspNetPager1.RecordCount = totalrecord;

                gvwProcureCatalog2.Visible = false;

                gvwProcureCatalog.Visible = true;

                gvwProcureCatalog.DataSource = dt;

                gvwProcureCatalog.DataBind();

                this.AspNetPager1.RecordCount = totalrecord;

            }
            catch (Exception e)
            {
                e.ToString();

                HZ.Web.WebUtil.Alert("请输入正确的查询格式！");
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            Databing();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
        }

        protected void lbnSwitch_Click(object sender, EventArgs e)
        {

        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {

            StringBuilder wherePm = new StringBuilder();

            wherePm = HZ.Supervise.Web.Function.GetConditionOfAreaId(wherePm, this.AreaControls1.SelectedAreaID) as StringBuilder;

            wherePm.AppendFormat("   AND daytime='{0}'", this.MaxforMonth1.Getvalue());

            StringBuilder wherePms = new StringBuilder();

            wherePms = HZ.Supervise.Web.Function.GetConditionOfAreaId(wherePms, this.AreaControls1.SelectedAreaID) as StringBuilder;

            string _oldYearmonth = this.MaxforMonth1.Getvalue().Insert(4, "-") + "-01";//根据本月拼接字符串，以便于能够转化成datetime类型，从而能够调用datetime类中求得上个月的方法

            wherePms.AppendFormat("  AND daytime='{0}'", Convert.ToDateTime(_oldYearmonth).AddMonths(-1).ToString("yyyyMM"));
            //修改者:蒋峰(2011-06-23)
            //备  注:现在用不上
            string sortSource = "1";

            if (this.dropMort.SelectedValue == "0" || this.dropMort.SelectedValue == "1" || this.dropMort.SelectedValue == "2")
            {

                wherePm.AppendFormat(" and SortSorce = {0}", this.dropMort.SelectedValue);

                wherePms.AppendFormat(" and SortSorce = {0}", this.dropMort.SelectedValue);

                sortSource = this.dropMort.SelectedValue;

            }

            string orderOfrank = "";

            string orderBy = "";

            if (this.ddlorderby.SelectedValue == "PurchaseAmount" || this.ddlorderby.SelectedValue == "DistributeAmount" || this.ddlorderby.SelectedValue == "WareHouseAmount")
            {

                orderOfrank = " sum(" + this.ddlorderby.SelectedValue + ")";

                orderBy = this.ddlorderby.SelectedValue + "total  " + this.dllorder.SelectedValue;

            }
            else
            {

                if (this.ddlorderby.SelectedValue == "WareHouseRate")
                {
                    orderOfrank = " (case when SUM(WarehouseAmount) = 0 then 0 when SUM(PurchaseAmount)=0 then 0 when CONVERT(decimal(18,3),CONVERT(float, SUM(WarehouseAmount)) / SUM(PurchaseAmount))>1 then 1 else  CONVERT(decimal(18,3),CONVERT(float, SUM(WarehouseAmount)) / SUM(PurchaseAmount)) end)";
                }
                else
                {

                    orderOfrank = " (case when SUM(DistributeAmount) = 0 then 0 when SUM(PurchaseAmount)=0 then 0 when CONVERT(decimal(18,3),CONVERT(float, SUM(DistributeAmount)) / SUM(PurchaseAmount))>1 then 1 else CONVERT(decimal(18,3),CONVERT(float, SUM(DistributeAmount)) / SUM(PurchaseAmount)) end)";

                }

                orderBy = this.ddlorderby.SelectedValue + " " + this.dllorder.SelectedValue;

            }

            int totalrecord = 0;

            string whereHosName = "";

            if (!string.IsNullOrEmpty(this.txtHospitalname.Text.Trim()))
            {

                whereHosName = " and A.HospitalName like '%" + DAL_Helper.Replace(txtHospitalname.Text.Trim()) + "%'";

            }

            DataTable dt = new BLL.BLL_PreWarning().GeteHospsRanking1(orderOfrank, orderBy, wherePm.ToString(), wherePms.ToString(), sortSource, whereHosName, 1, 0, out totalrecord);

            gvwProcureCatalog.Visible = true;

            gvwProcureCatalog2.Visible = false;

            Function.ChangeRate(dt, "WarehouseRate", "addWarehouseRate");

            Function.ChangeRate(dt, "DistributeRate", "addDistributeRate");

            Function.AddColumn(dt, "SortSource");

            Function.AddColRank(dt, "pms");

            ExportFile.Instance.ExportData(dt, gvwProcureCatalog, this);

        }

        protected void lbtnSee_Click(object sender, EventArgs e)
        {

        }
    }
}
